package br.com.klis.easycontrast.presenter;

import br.com.klis.easycontrast.model.ContrastSession;
import br.com.klis.easycontrast.view.ContrastSessionViewInterface;

public class ContrastSessionPresenter implements ContrastSessionViewInterface.Presenter{
	private ContrastSessionViewInterface viewInterface;
	private ContrastSession contrastSession;

	public ContrastSessionPresenter(ContrastSessionViewInterface view, ContrastSession contrastSession){
		this.viewInterface = view;
		this.contrastSession = contrastSession;
	}

	@Override
	public void play() {
		 viewInterface.startCountDownTimer();
		 viewInterface.setRunningMode();
	}

	@Override
	public void pause() {
		viewInterface.stopCountDownTimer();
		viewInterface.setPauseMode();
	}

	@Override
	public void restart() {
		viewInterface.stopCountDownTimer();
		updateTimeScreen(true);
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.klis.easycontrast.view.ContrastSessionViewInterface.Presenter#updateTimeScreen()
	 */
	@Override
	public void updateTimeScreen(boolean resetTime){
		if(resetTime){
			viewInterface.setInitMode();
			viewInterface.updateTextView(contrastSession.getContrastModeAtual().getTime(), contrastSession.getContrastModeAtual().getTimeSec());
		}else{
			viewInterface.restoreMode();
		}
		viewInterface.updateContrastMode(contrastSession.getContrastModeAtual());
	}
	
	@Override
	public void initProgressBar(){
		viewInterface.initProgressBar(contrastSession.getContrastModeAtual().getTime(),contrastSession.getContrastModeAtual().getTimeSec());
	}
	
	public void startCountDown() {
    	viewInterface.startCountDownTimer();
    }
	
	public void cancelCountDown() {
    	viewInterface.stopCountDownTimer();
    }

	@Override
	public void startSession() {
		contrastSession.initTime();
		contrastSession.initRepetition();
		contrastSession.initContrastMode();
		createTime();
		
	}
	
	@Override
	public void startScreen(boolean resetTime){
		updateRepetitionText();
		updateTimeScreen(resetTime);
		initProgressBar();
	}
	
	@Override
	public void restartSession() {
		viewInterface.stopCountDownTimer();
		startSession();
		startScreen(true);
	}
	
	@Override
	public void createTime(){
		contrastSession.incrementTime();
	}
	
	@Override
	public void updateRepetitionText(){
		viewInterface.updateRepetitionText(contrastSession.getRepetition(),contrastSession.getConfiguration().getRepetitions());
	}

	@Override
	public String getContrastModeAtualText(){
		return Integer.valueOf(contrastSession.getContrastModeAtual().getText()).toString();
	}
	@Override
	public int getRepetitionCount(){
		return contrastSession.getRepetition();
	}
	@Override
	public boolean isNewRepetition() {
		return contrastSession.isNewRepetition();
	}
	@Override
	public boolean isLastTimeOfSession() {
		return contrastSession.isLastTimeOfSession();
	}

	@Override
	public boolean playSound() {
		return contrastSession.getConfiguration().isPlaySound();
	}

	@Override
	public boolean vibrate() {
		return contrastSession.getConfiguration().isVibrate();
	}
}
